<?php

declare(strict_types=1);

namespace App\Controller\admin;

use App\Constants\ErrorCode;
use App\Controller\AbstractController;
use App\Model\admin\SystemRole;
use App\Model\admin\SystemRolePermission;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Di\Annotation\Inject;
use Psr\Http\Message\ResponseInterface;

/**
 * 系统角色
 * Class SystemRoleController
 * @package App\Controller\admin
 * @Controller(prefix="admin")
 */
class SystemRoleController extends AbstractController
{
    /**
     * @Inject
     * @var SystemRole
     */
    protected $model;

    /**
     * 列表
     * @RequestMapping(path="role",methods="get")
     * @return ResponseInterface
     */
    public function index(): ResponseInterface
    {
        $where['status'] = 1;
        $list = $this->model->query()->where($where)->orderBy('id','desc')->get();
        $total = $this->model->query()->where($where)->count();
        return $this->success([
            'list' => $list,
            'total' => $total
        ]);
    }

    /**
     * 保存
     * @RequestMapping(path="role",methods="post")
     */
    public function store(): ResponseInterface
    {
        $data = $this->request->all();
        $validator = $this->validationFactory->make($data,['title' => 'required']);

        if ($validator->fails()){
            $errorMessage = $validator->errors()->first();
            return $this->fail(ErrorCode::BUSINESS_FAIL,$errorMessage);
        }
        if ($this->model->create($data))
            return $this->success();

        return $this->fail(ErrorCode::BUSINESS_FAIL, '保存失败');
    }

    /**
     * 详情
     * @RequestMapping(path="role/{id}",methods="get")
     */
    public function show($id): ResponseInterface
    {
        $info = $this->model->query()->find($id);
        $info['auth'] = $this->container->get(SystemRolePermission::class)->getAuthByRoleId($info['id']);
        return $this->success($info);
    }

    /**
     * 更新
     * @RequestMapping(path="role",methods="put")
     */
    public function update(): ResponseInterface
    {
        $data = $this->request->all();
        $validator = $this->validationFactory->make($data,['title' => 'required']);

        if ($validator->fails()){
            $errorMessage = $validator->errors()->first();
            return $this->fail(ErrorCode::BUSINESS_FAIL,$errorMessage);
        }
        $info = $this->model->query()->find($data['id']);

        if (!$info) return $this->fail(ErrorCode::BUSINESS_FAIL,'不存在');

        if ($info->update($data))
            return $this->success();

        return $this->fail(ErrorCode::BUSINESS_FAIL, '保存失败');
    }

    /**
     * 删除
     * @RequestMapping(path="role/{id}",methods="delete")
     */
    public function destroy($id): ResponseInterface
    {
        if ($this->model->where('id', $id)->delete()) {
            SystemRolePermission::where('role_id',$id)->delete(); // 把权限也删除了
            return $this->success();
        }
        return $this->fail(ErrorCode::BUSINESS_FAIL,'删除失败');
    }
}

